#include "configdialog.h"

ConfigDialog::ConfigDialog(QWidget *parent) : QDialog(parent) {
	ui.setupUi(this);
	ui.spinBox->setRange(5, 16);

	{
	QSettings settings;
	QVariant defc;
	bool ok;
	defc.setValue<QColor>(QColor(127,0,255,127));
	rectColor=settings.value(tr("rectColor"), defc).value<QColor>();
	clickPointColor=settings.value(tr("clickPointColor"), defc).value<QColor>();
	defc.setValue<QColor>(Qt::black);
	textColor=settings.value(tr("textColor"), defc).value<QColor>();
	ui.fontComboBox->setCurrentFont(settings.value(tr("textFont")).value<QFont>());
	ui.spinBox->setValue(settings.value("fontSize", 9).toInt(&ok));
	bool icon=settings.value("noteIcon", true).toBool();
	if (icon) ui.noteIconRadioButton->setChecked(true); else ui.clickPointRadioButton->setChecked(true);
	ui.changeClickPointColorButton->setDisabled(icon);
	}

	connect(ui.buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
	connect(ui.buttonBox, SIGNAL(accepted()), this, SLOT(onAcceptClicked()));
	connect(ui.buttonBox, SIGNAL(clicked(QAbstractButton *)), this, SLOT(onButtonClicked(QAbstractButton *)));
	connect(ui.clickPointRadioButton, SIGNAL(toggled(bool)), ui.changeClickPointColorButton, SLOT(setEnabled(bool)));

	updateColors();
}

ConfigDialog::~ConfigDialog() {

}

void ConfigDialog::onButtonClicked(QAbstractButton * button) {
	if (QDialogButtonBox::ApplyRole==ui.buttonBox->buttonRole(button)) saveSettings();
}

void ConfigDialog::onAcceptClicked() {
	saveSettings();
	accept();
}

void ConfigDialog::on_changeFrameColorButton_clicked() {
	QColor c=QColorDialog::getColor(rectColor, this);
	if (c.isValid()) rectColor=c;
	updateColors();
}
void ConfigDialog::on_changeClickPointColorButton_clicked(){
	QColor c=QColorDialog::getColor(clickPointColor, this);
	if (c.isValid()) clickPointColor=c;
	updateColors();
}

void ConfigDialog::on_changeTextColorButton_clicked(){
	QColor c=QColorDialog::getColor(textColor, this);
	if (c.isValid()) textColor=c;
	updateColors();
}

void ConfigDialog::updateColors() {
	cPal.setColor(ui.frameColorLabel->backgroundRole(), rectColor);
	ui.frameColorLabel->setPalette(cPal);
	cPal.setColor(ui.clickPointColorLabel->backgroundRole(), clickPointColor);
	ui.clickPointColorLabel->setPalette(cPal);
	cPal.setColor(ui.noteTextColorLabel->backgroundRole(), textColor);
	ui.noteTextColorLabel->setPalette(cPal);
}

void ConfigDialog::saveSettings() {
	QSettings settings;
	QVariant var;
	var.setValue<QColor>(rectColor);
	settings.setValue("rectColor", var);
	var.setValue<QColor>(clickPointColor);
	settings.setValue("clickPointColor", var);
	var.setValue<QColor>(textColor);
	settings.setValue("textColor", var);
	var.setValue<QFont>(ui.fontComboBox->currentFont());
	settings.setValue("noteIcon", ui.noteIconRadioButton->isChecked());
	settings.setValue("textFont", var);
	settings.setValue("fontSize", ui.spinBox->value());

}
